﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EPE.WPF
{
    /// <summary>
    /// DBConfigForm.xaml 的交互逻辑
    /// </summary>
    public partial class DBConfigForm : Window
    {
        public DBConfigForm()
        {
            InitializeComponent();
            LoadConfig();
        }


        private void LoadConfig()
        {
            string strConnectionString = GetAppConfig("ConnectionString");

            string[] strSplit = strConnectionString.Split(';');

            if (strSplit.Length > 0)
            {
                this.txtServer.Text = strSplit[0].Split('=')[1].ToString();
                this.txtDatabase.Text = strSplit[1].Split('=')[1].ToString();
                this.txtUser.Text = strSplit[2].Split('=')[1].ToString();
                this.txtPwd.Password = strSplit[3].Split('=')[1].ToString();
                this.cmbCharset.Text = strSplit[4].Split('=')[1].ToString().Trim();
                this.txtPort.Text = strSplit[5].Split('=')[1].ToString().Trim();
            }

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {

            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            string strConnectionString = "Server=" + this.txtServer.Text + ";Database=" + this.txtDatabase.Text + ";User=" + this.txtUser.Text + ";pwd=" + this.txtPwd.Password + ";charset=" + this.cmbCharset.SelectionBoxItem + ";Port=" + this.txtPort.Text;
            UpdateAppConfig("ConnectionString", strConnectionString);

            this.Close();
        }

        ///<summary> 
        ///返回＊.exe.config文件中appSettings配置节的value项  
        ///</summary> 
        ///<param name="strKey"></param> 
        ///<returns></returns> 
        private static string GetAppConfig(string strKey)
        {
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == strKey)
                {
                    return ConfigurationManager.AppSettings[strKey];
                }
            }
            return null;
        }

        ///<summary>  
        ///在＊.exe.config文件中appSettings配置节增加一对键、值对  
        ///</summary>  
        ///<param name="newKey"></param>  
        ///<param name="newValue"></param>  
        private static void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }

            // Open App.Config of executable  
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // You need to remove the old settings object before you can replace it  
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            // Add an Application Setting.  
            config.AppSettings.Settings.Add(newKey, newValue);
            // Save the changes in App.config file.  
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.  
            ConfigurationManager.RefreshSection("appSettings");
        } 

    }
}
